package com.ghxxzx.daglcustom.business_form.web;

import com.ghxxzx.daglcustom.business_form.domain.FormTypePo;
import com.ghxxzx.daglcustom.business_form.domain.dto.FormTypeDto;
import com.ghxxzx.daglcustom.business_form.domain.dto.FormTypeQuery;
import com.ghxxzx.daglcustom.business_form.service.FormTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.tmsps.ne4spring.base.BaseAction;
import org.tmsps.ne4spring.base.ne.NePage;
import org.tmsps.ne4spring.utils.wrapper.WrapMapper;
import org.tmsps.ne4spring.utils.wrapper.Wrapper;

import javax.validation.Valid;
import java.text.ParseException;

@RestController
@RequestMapping("/formType")
public class FormTypeAction extends BaseAction {

	@Autowired
	private FormTypeService formTypeService;

	@GetMapping("/list")
	public Wrapper<NePage> list(@Valid FormTypeQuery formTypeQuery) {
		NePage page = formTypeService.list(formTypeQuery);
		return WrapMapper.ok(page);
	}

	@PostMapping("/add")
	public Wrapper<String> add(@Valid FormTypeDto formTypeDto) throws ParseException {
		FormTypePo formTypePo = new FormTypePo();
		formTypePo.setCode(formTypeDto.getCode());
		formTypePo.setType(formTypeDto.getType());
		bs.saveObj(formTypePo);
		return WrapMapper.ok("添加成功");
	}

	@PostMapping("/edit_form")
	public Wrapper<FormTypePo> editForm(@Valid String kid) {
		FormTypePo formTypePo = bs.findById(kid, FormTypePo.class);
		return WrapMapper.ok(formTypePo);
	}

	@PostMapping("/edit")
	public Wrapper<String> edit(@Valid FormTypeDto formTypeDto) throws ParseException {
		FormTypePo formTypePo = bs.findById(formTypeDto.getKid(),FormTypePo.class);
		formTypePo.setType(formTypeDto.getType());
		formTypePo.setCode(formTypeDto.getCode());
		bs.updateObj(formTypePo);
		return WrapMapper.ok("修改成功");
	}

	@GetMapping("/delete")
	public Wrapper<String> delete(@Valid String kid) {
		bs.deleteById(kid, FormTypePo.class);
		return WrapMapper.ok("删除成功");
	}
	
}
